package com.yyw.mcar.base;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * Created by wang on 2015/6/9.
 */
public abstract class MsBaseAdapter extends BaseAdapter {

	public Context mContext;
	public List<Object> modelList;
	public LayoutInflater mLayoutInflater;// 反射器

	public MsBaseAdapter(Context context, List<Object> modelList) {
		this.mContext = context;
		this.modelList = modelList;
		mLayoutInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		if (modelList != null) {
			return modelList.size();
		} else {
			return 0;
		}

	}

	@Override
	public Object getItem(int position) {
		if (modelList != null) {
			return modelList.get(position);
		} else {
			return null;
		}

	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = bindView(position, convertView, parent);
		bindData(position, convertView, parent);
		bindEvent(position, convertView, parent);
		return convertView;
	}

	/**
	 * release memory -- add By Melvin
	 */
	public void destroyAdapter() {

		if (mLayoutInflater != null) {
			mLayoutInflater = null;
		}
		if (mContext != null) {
			mContext = null;
		}

	}

	/**
	 * 绑定列表的视图
	 * 
	 * @param position
	 * @param convertView
	 * @param parent
	 */
	public abstract View bindView(int position, View convertView,
			ViewGroup parent);

	/**
	 * 绑定列表的数据
	 * 
	 * @param position
	 * @param convertView
	 * @param parent
	 */
	public abstract void bindData(int position, View convertView,
			ViewGroup parent);

	/**
	 * 绑定列表中的事件
	 * 
	 * @param position
	 * @param convertView
	 * @param parent
	 */
	public abstract void bindEvent(int position, View convertView,
			ViewGroup parent);

}
